package views.createViews;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import controllers.createControllers.ToevoegenParameterController;
import controllers.updateControllers.UpdateParameterController;
import core.logic.services.utilServices.ValidatieService;
import core.models.dataModels.createModels.ToevoegenParameterModel;
import core.models.domainModel.Parameter;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;

//Pieter Gielen C1025696

public class ToevoegenParameterView extends JFrame {
	private static final long serialVersionUID = 1L;
	private ToevoegenParameterModel model;
	private JTextField txtParameterType;
	private JTextField txtParameterDetail;
	private JTextField txtOmschrijving;
	private JTextField txtWaardeNummeriek;
	private JTextField txtWaardeTekst;
	private JButton btnOK;
	private JButton btnCancel;	
	
	public ToevoegenParameterView(ToevoegenParameterModel _model) 
	{
		model = _model;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		//FORMULIER
		//====================
		txtParameterType = new JTextField();
		txtParameterType.setColumns(10);		
		txtParameterDetail = new JTextField();			
		txtParameterDetail.setColumns(10);
		txtOmschrijving = new JTextField();
		txtOmschrijving.setColumns(10);		
		txtWaardeNummeriek = new JTextField();
		txtWaardeNummeriek.setColumns(10);		
		txtWaardeTekst = new JTextField();
		txtWaardeTekst.setColumns(10);	
		JLabel lblNewLabel = new JLabel("ParameterType");	
		JLabel lblParameterdetail = new JLabel("ParameterDetail");
		JLabel lblOmschrijving = new JLabel("Omschrijving");
		JLabel lblWaardenummeriek = new JLabel("WaardeNummeriek  (DOUBLE !!)");
		JLabel lblWaardetekst = new JLabel("WaardeTekst");
			
		switch(model.getSchermModus())
		{
		case "CREATE":
			setTitle("Parameter Toevoegen");
			break;
		case "UPDATE":
			setTitle("Parameter Wijzigen");
			txtParameterDetail.setText(model.get_parameterEnkel().getParameterDetail());
			txtParameterType.setText(model.get_parameterEnkel().getParameterType());
			txtOmschrijving.setText(model.get_parameterEnkel().getOmschrijving());
			txtWaardeNummeriek.setText(model.get_parameterEnkel().getWaardeNummeriek().toString());
			txtWaardeTekst.setText(model.get_parameterEnkel().getWaardeTekst());
			break;
		}
		
		// KNOP OK
		//====================
		//data uit btn halen en doorgeven aan controller
		btnOK = new JButton("OK");
		btnOK.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				switch(model.getSchermModus())
				{
				case "CREATE":
					//bij create:
					//waardes controleren gaat pas na validatie klasse en moet dus nog gebeuren!
					if(validatieOk())
					{
						//Parameter Param = new Parameter(txtParameterType.getText(), txtParameterDetail.getText(),txtOmschrijving.getText(), Double.parseDouble(txtWaardeNummeriek.getText()), txtWaardeTekst.getText()  );					
						Parameter Param = new Parameter(txtParameterType.getText(),txtParameterDetail.getText(), txtOmschrijving.getText(), 0.0, "");
						if(txtWaardeNummeriek.getText().length()>0)
						{
							Param.setWaardeNummeriek(Double.parseDouble(txtWaardeNummeriek.getText()));
						}
						if(txtWaardeTekst.getText().length()>0)
						{
							Param.setWaardeTekst(txtWaardeTekst.getText());
						}
						model.set_parameterEnkel(Param);					
						if (ToevoegenParameterController.handleView(model))
						{
							JOptionPane.showMessageDialog(null, "Het toevoegen is gelukt!");
						}
						else 
						{
							JOptionPane.showMessageDialog(null, "Het toevoegen is NIET gelukt!");
						}
					}
					break;
				case "UPDATE":
					if (validatieOk())
					{
						//public Parameter(String parameterType, String parameterDetail,
						//String omschrijving, Double waardeNummeriek, String waardeTekst)

						Parameter nieuweParam = new Parameter(
								txtParameterType.getText(),
								txtParameterDetail.getText(),
								txtOmschrijving.getText(),
								Double.parseDouble(txtWaardeNummeriek.getText()),
								txtWaardeTekst.getText());
						
						if (UpdateParameterController.handleView(nieuweParam, model.get_parameterEnkel()))
						{
							JOptionPane.showMessageDialog(null, "Het updaten is gelukt!");
						}
						else
						{
							JOptionPane.showMessageDialog(null, "Het updaten is NIET gelukt!");
						}
						
					}
					break;
				}
				
				
			}
		});
		
		//KNOP CANCEL
		//=========================		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				switch(model.getSchermModus())
				{
				case "CREATE":
					ToevoegenParameterController.closeView();
				case "UPDATE":
					UpdateParameterController.closeView();					
				}
			}
		});

		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(19)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnOK, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
							.addGap(37)
							.addComponent(btnCancel, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblWaardetekst)
								.addComponent(lblWaardenummeriek)
								.addComponent(lblOmschrijving)
								.addComponent(lblParameterdetail)
								.addComponent(lblNewLabel))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(txtParameterDetail)
								.addComponent(txtParameterType, GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
								.addComponent(txtOmschrijving)
								.addComponent(txtWaardeTekst)
								.addComponent(txtWaardeNummeriek, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap(23, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(txtParameterType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblParameterdetail)
						.addComponent(txtParameterDetail, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblOmschrijving)
						.addComponent(txtOmschrijving, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblWaardenummeriek)
						.addComponent(txtWaardeNummeriek, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblWaardetekst)
						.addComponent(txtWaardeTekst, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(32)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(btnCancel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(btnOK, GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
					.addContainerGap(41, Short.MAX_VALUE))
		);
		

		getContentPane().setLayout(groupLayout);

	}
	public boolean validatieOk()
	{
		if(!ValidatieService.isPresentAndLength(txtOmschrijving, 150, "Omschrijving" ))
		{
			return false;
		}
		if(!ValidatieService.isPresentAndLength(txtParameterDetail, 50, "Parameter detail"))
		{
			return false;
		}
		if(!ValidatieService.isPresentAndLength(txtParameterType, 50, "Parameter type"))
		{
			return false;
		}
		if(txtWaardeNummeriek.getText().length()>0)
		{
			if(!ValidatieService.isDouble(txtWaardeNummeriek, 18, "Waarde numeriek"))
			{
				return false;
			}
		}
		if(txtWaardeTekst.getText().length()>0)
		{
			if(!ValidatieService.isPresentAndLength(txtWaardeTekst, 150, "Waarde tekst"))
			{
				return false;
			}
		}
		
		return true;
	}
	
	
	
}




